home *** CD-ROM | disk | FTP | other *** search
/ Ultimedia 2 / Ultimedia 2.iso / tools / animplayer / amipeg / source.lha / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-03  |  7.0 KB  |  339 lines

  1. /*
  2.  *  This is the main part
  3.  */
  4.  
  5. #include "video.h"
  6. #include "proto.h"
  7. #include <sys/types.h>
  8. #include <signal.h>
  9.  
  10. #include "util.h"
  11.  
  12. /* Define buffer length. */
  13.  
  14. #define BUF_LENGTH 65536
  15.  
  16.  
  17. /* Declaration of global variable to hold dither info. */
  18.  
  19. int ditherType;
  20.  
  21. /* Global file pointer to incoming data. */
  22. FILE *input;
  23.  
  24. /* End of File flag. */
  25. static int EOF_flag = 0;
  26.  
  27. /* Loop flag. */
  28. int loopFlag = 0;
  29.  
  30. /* Quiet flag. */
  31. int quietFlag = 0;
  32.  
  33. /* Display image on screen? */
  34. int noDisplayFlag = 0;
  35.  
  36. /* Setjmp/Longjmp env. */
  37. jmp_buf env;
  38.  
  39. /* Method of picture conversion */
  40. void (*DoDitherImage)(unsigned char *l, unsigned char *Cr, unsigned char *Cb,
  41.               unsigned char *disp, int h, int w);
  42.  
  43. static char version[]="$VER: aMiPEG 0.2 (compiled on " __DATE__ ", " __TIME__ ")\n";
  44.  
  45. /*
  46.  *--------------------------------------------------------------
  47.  *
  48.  * get_more_data --
  49.  *
  50.  *    Called by correct_underflow in bit parsing utilities to
  51.  *      read in more data.
  52.  *
  53.  * Results:
  54.  *    Input buffer updated, buffer length updated.
  55.  *      Returns 1 if data read, 0 if EOF, -1 if error.
  56.  *
  57.  * Side effects:
  58.  *      None.
  59.  *
  60.  *--------------------------------------------------------------
  61.  */
  62.  
  63. int get_more_data(unsigned int *buf_start, int max_length, int *length_ptr, unsigned int **buf_ptr)
  64. {
  65.   
  66.   int length, num_read, request;
  67.   unsigned char *buffer, *mark;
  68.  
  69.   if (EOF_flag) return 0;
  70.  
  71.   length = *length_ptr;
  72.   buffer = (unsigned char *) *buf_ptr;
  73.  
  74.   if (length > 0) {
  75.     memcpy((unsigned char *) buf_start, buffer, (length*4));
  76.     mark = ((unsigned char *) (buf_start + length));
  77.   }
  78.   else {
  79.     mark = (unsigned char *) buf_start;
  80.     length = 0;
  81.   }
  82.  
  83.   request = (max_length-length)*4;
  84.   
  85.   num_read = fread( mark, 1, request, input);
  86.  
  87.   /* Paulo Villegas - 26/1/1993: Correction for 4-byte alignment */
  88.   {
  89.     int num_read_rounded;
  90.     unsigned char *index;
  91.  
  92.     num_read_rounded = num_read & 0xfffffffc;
  93.  
  94.     /* this can happen only if num_read<request; i.e. end of file reached */
  95.     if( num_read_rounded < num_read )
  96.       { 
  97.      num_read_rounded+=4;
  98.      /* fill in with zeros */
  99.      for( index=mark+num_read; index<mark+num_read_rounded; *(index++)=0 );
  100.      /* advance to the next 4-byte boundary */
  101.      num_read = num_read_rounded;
  102.       }
  103.   }
  104.   
  105.   if (num_read < 0) {
  106.     return -1;
  107.   }
  108.   else if (num_read == 0) {
  109.     *buf_ptr = buf_start;
  110.     
  111.     /* Make 32 bits after end equal to 0 and 32
  112.        bits after that equal to seq end code
  113.        in order to prevent messy data from infinite
  114.        recursion.
  115.     */
  116.  
  117.     *(buf_start + length) = 0x0;
  118.     *(buf_start + length+1) = SEQ_END_CODE;
  119.  
  120.     EOF_flag = 1;
  121.     return 0;
  122.   }
  123.  
  124.   num_read >>= 2;
  125.  
  126.   *buf_ptr = buf_start;
  127.   *length_ptr = length + num_read;
  128.  
  129.   return 1;
  130. }
  131.  
  132. /*
  133.  *--------------------------------------------------------------
  134.  *
  135.  * int_handler --
  136.  *
  137.  *    Handles Cntl-C interupts..
  138.  *
  139.  * Results:
  140.  *    None.
  141.  *
  142.  * Side effects:
  143.  *    None.
  144.  *
  145.  *--------------------------------------------------------------
  146.  */
  147. void int_handler(int dummy)
  148. {
  149.     if (!quietFlag) fprintf(stderr, "Interrupted!\n");
  150.     if (curVidStream) DestroyVidStream(curVidStream);
  151.     exit(1);
  152. }
  153.  
  154.  
  155. /*
  156.  *--------------------------------------------------------------
  157.  *
  158.  * main --
  159.  *
  160.  *    Parses command line, starts decoding and displaying.
  161.  *
  162.  * Results:
  163.  *    None.
  164.  *
  165.  * Side effects:
  166.  *    None.
  167.  *
  168.  *--------------------------------------------------------------
  169.  */
  170.  
  171. void main(int argc, char **argv)
  172. {
  173.  
  174.   char *name;
  175.   static VidStream *theStream;
  176.   int mark;
  177.   extern int lores;
  178.  
  179.   mark = 1;
  180.   argc--;
  181.  
  182.   name = "";
  183.   input = stdin;
  184.   ditherType = FULL_COLOR_DITHER;
  185.   noDisplayFlag = 0;
  186.  
  187.   while (argc) {
  188.     if (strcmp(argv[mark], "-nop") == 0) {
  189.       TogglePFlag();
  190.       argc--; mark++;
  191.     } else if (strcmp(argv[mark], "-nob") == 0) {
  192.       ToggleBFlag();
  193.       argc--; mark++;
  194.     } else if (strcmp(argv[mark], "-display") == 0) {
  195.       name = argv[++mark];
  196.       argc -= 2; mark++;
  197.     } else if (strcmp(argv[mark], "-dither") == 0) {
  198.       argc--; mark++;
  199.       if (argc < 1) {
  200.     perror("Must specify dither option after -dither flag");
  201.     usage(argv[0]);
  202.       }
  203.       if (strcmp(argv[mark], "gray") == 0) {
  204.     argc--; mark++;
  205.     ditherType = GRAY_DITHER;
  206.       } else if (strcmp(argv[mark], "color") == 0) {
  207.     argc--; mark++;
  208.     ditherType = FULL_COLOR_DITHER;
  209.       } else if (strcmp(argv[mark], "none") == 0) {
  210.     argc--; mark++;
  211.     ditherType = NO_DITHER;
  212.       } else {
  213.     perror("Illegal dither option.");
  214.     usage(argv[0]);
  215.       }
  216.     } 
  217.     else if (strcmp(argv[mark], "-eachstat") == 0) {
  218.       argc--; mark++;
  219. #ifdef ANALYSIS
  220.       showEachFlag = 1;
  221. #else
  222.       fprintf(stderr, "To use -eachstat, recompile with -DANALYSIS in CFLAGS\n");
  223.       exit(1);
  224. #endif
  225.     }
  226.     else if (strcmp(argv[mark], "-quiet") == 0) {
  227.       argc--; mark++;
  228.       quietFlag = 1;
  229.     }
  230.     else if (strcmp(argv[mark], "-loop") == 0) {
  231.       argc--; mark++;
  232.       loopFlag = 1;
  233.     }
  234.     else if (strcmp(argv[mark], "-no_display") == 0) {
  235.       argc--; mark++;
  236.       noDisplayFlag = 1;
  237.     }
  238.     else if (argv[mark][0] == '-') {
  239.       fprintf(stderr, "Un-recognized flag %s\n",argv[mark]);
  240.       usage(argv[0]);
  241.     }
  242.     else {
  243.       input = fopen(argv[mark], "r");
  244.       if (input == NULL) {
  245.     fprintf(stderr, "Could not open file %s\n", argv[mark]);
  246.     usage(argv[0]);
  247.       }
  248.       argc--; mark++;
  249.     }
  250.   }
  251.  
  252.   signal(SIGINT, int_handler);
  253.  
  254.   init_tables();
  255.   
  256.   switch (ditherType) {
  257.     case GRAY_DITHER:
  258.         InitGrayDisplay(name);
  259.         break;
  260.  
  261.     case FULL_COLOR_DITHER:
  262.         InitColorDither();
  263.         InitColorDisplay(name);
  264.         break;
  265.  
  266.     case NO_DITHER:
  267.         HAM8_draw = (void (*)(void *, int, int)) NoDitherImage;    // method casting ... argh!
  268.         DoDitherImage = NoDitherImage;
  269.         break;
  270.   }
  271.  
  272. /*
  273.  *  The new restart handling has not been checked out very closely with changing
  274.  *  (non)intra scale matrices!
  275.  */
  276.  
  277.   theStream = NewVidStream(BUF_LENGTH);
  278.  
  279.   if (setjmp(env) != 0) {
  280.     mpegInitVidRsrc(); /* fix bug in static first in mpegVidRsrc */
  281.  
  282.     rewind(input);
  283.  
  284.     EOF_flag = 0;
  285.     theStream->bit_offset = 0;
  286.     theStream->buf_length = 0;
  287.     theStream->buffer = NULL;
  288.     totNumFrames = 0;
  289. #ifdef ANALYSIS 
  290.     init_stats();
  291. #endif
  292.   }
  293.  
  294.   realTimeStart = ReadSysClock();
  295.   while (mpegVidRsrc(0, theStream));
  296. }
  297.  
  298.  
  299. /*
  300.  *--------------------------------------------------------------
  301.  *
  302.  * usage --
  303.  *
  304.  *    Print mpeg_play usage
  305.  *
  306.  * Results:
  307.  *    None.
  308.  *
  309.  * Side effects:
  310.  *    exits with a return value -1
  311.  *
  312.  *--------------------------------------------------------------
  313.  */
  314.  
  315. void usage(char *s)  /* program name */
  316. {
  317.     fprintf(stderr, "Usage:\n");
  318.     fprintf(stderr, "mpeg_play\n");
  319.     fprintf(stderr, "          [-nob]\n");
  320.     fprintf(stderr, "          [-nop]\n");
  321.     fprintf(stderr, "          [-dither {gray|color|none}]\n");
  322.     fprintf(stderr, "          [-loop]\n");
  323.     fprintf(stderr, "          [-eachstat]\n");
  324.     fprintf(stderr, "          [-no_display]\n");
  325.     fprintf(stderr, "          [-quiet]\n");
  326.     fprintf(stderr, "          file_name\n");
  327.     exit (-1);
  328. }
  329.  
  330.  
  331. /*
  332.  *  Dummy display method
  333.  *
  334.  */
  335. void NoDitherImage(unsigned char *l, unsigned char *Cr, unsigned char *Cb,
  336.            unsigned char *disp, int h, int w)
  337. {}
  338.  
  339.